//
//  SecurityLegalVC.swift
//  SBSafetyApp
//
//  Created by Lina on 2022/11/15.
//
// 【首页-电子台账】安全保卫法制宣传教育记录

import Foundation
import UIKit

class SecurityLegalVC: BaseFormVC {
    let listV = SecurityLegalView()

    // MARK: - Override
    override func reloadData() {
        listV.deptName = deptName ?? ""
        listV.registerField(withValidator: validator)
    }
   
    override func didValidated(withMode mode: String?) {

        view.showToastActivity()
        // 完成校验
        var valPam = ElecParam(bookType: modal.typeValue)
        valPam.tjrq = Date.dateToUp(listV.val0TF.value ?? "")
        valPam.attrValue0 = Date.dateToUp(listV.val0TF.value ?? "")
        valPam.attrValue1 = listV.val1TF.value
        valPam.attrValue2 = listV.val2TF.value
        valPam.attrValue3 = listV.val3TF.value
        valPam.attrValue4 = listV.val4TF.value
        valPam.attrValue5 = ""
        valPam.attrValue6 = listV.val6TF.value
        
        var count = 0
        var fjList: [FjdzParam] = []
        let imgs = listV.val7TF.imgs
        if imgs.count > 0 {
            for img in imgs {
                if let v1Path = FilesManager.shared.saveImage(img: img.resizedTo1MB() ?? img) {
                    API.postStandingBookUpload(withImage: v1Path) { responseModel in
                        if let url = responseModel.model?.imgUrl {
                            fjList.append(FjdzParam(fjdz: url))
                        }
                        count += 1
                        if count == imgs.count {
                            valPam.fjList = fjList
                            self.didUpload(withData: valPam)
                        }
                    }
                }
            }
        } else {
            self.didUpload(withData: valPam)
        }
    }
    
    func didUpload(withData valPam: ElecParam) {
        do {
            var jsonParam = CommonJsonParam()
            let data: Data = try JSONEncoder().encode(valPam)
            let string = String(data: data, encoding: String.Encoding.utf8)
            jsonParam.jsonStr = string
            API.postStandingBookSave(withJson: jsonParam) { responseModel in
                if responseModel.errorCode == .Success {
                    self.view.showToast(witMsg: responseModel.errorMessage)
                    self.navigationController?.popViewController(animated: true)
                }
                self.view.hideToastActivity()
            }
            
        } catch {
            self.view.hideToastActivity()
            print("SecurityLegalVC didValidated error", error)
        }
    }
    
    override func setupUI() {
        listV.submitBtn.addTarget(self, action: #selector(submitTapped), for: .touchUpInside)
        view.addSubview(listV)
        listV.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
        }
    }
}
